package b1.scenes.home
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	import b1.utils.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.text.*;
	import flash.ui.Mouse;
	import flash.utils.Timer;
	
	public class HeirloomBox extends Sprite
	{
		public var fullBones:Sprite;
		public var halfBones:Sprite;
		public var emptyBox:Sprite;
		public var ownerInfo:PlayerInfo;
		public var mouseOverMessage:Sprite;
		private var mouseOverFlag:Boolean;
		private var tipText:TextField;
		
		public function HeirloomBox(playerInfo:PlayerInfo)
		{
			mouseOverFlag = false;
			ownerInfo = playerInfo;
			var EmptyHeirloomBox:Class = ResourceHelper.getInstance().getClassByName("EmptyHeirloomBox");
			emptyBox = new EmptyHeirloomBox();
			emptyBox.width = 100;
			emptyBox.height = 89;	
			addChild(emptyBox);
			var FullBones:Class = ResourceHelper.getInstance().getClassByName("FullBones");
			fullBones = new FullBones();
			fullBones.scaleX = emptyBox.scaleX;
			fullBones.scaleY = emptyBox.scaleY;	
			addChild(fullBones);
			var HalfBones:Class = ResourceHelper.getInstance().getClassByName("HalfBones");
			halfBones = new HalfBones();
			halfBones.scaleX = emptyBox.scaleX;
			halfBones.scaleY = emptyBox.scaleY;	
			addChild(halfBones);
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			this.addEventListener(MouseEvent.CLICK, onClick);
			var timer:Timer = new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER, onTick);
			timer.start();
			refresh();
		}
		
		private function onTick(event:TimerEvent):void
		{
			refresh();
		}
		
		private function onClick(event:Event):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			if (ownerInfo.heirloomBoxBoneLeft <= 0)
				return;
			var boneBalance:String = new String();
			var ownerRankInfo:RankConfig = ownerInfo.getRankInfo();
			var getBones:int = 0;
			if (PlayerInfoHelper.getInstance().userId == ownerInfo.id)
			{
				getBones = ownerInfo.heirloomBoxBoneLeft;	
			}
			else
			{
				if (ownerInfo.heirloomBoxBoneLeft >= ownerRankInfo.heirloomBoxVolume)
				{
					var liveAnimalIntelCount:int = 0;
					for (var i:int = 0; i < ownerInfo.liveAnimals.length; i++)
					{
						var liveAnimal:AnimalInfo =  PlayerInfoHelper.getInstance().animals[ownerInfo.liveAnimals[i]];
						liveAnimalIntelCount += liveAnimal.intelligence;
					}
					var me:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
					var myAnimal:AnimalInfo = PlayerInfoHelper.getInstance().animals[me.ownAnimals[0]];
					getBones = Math.max(ownerInfo.heirloomBoxBoneLeft * myAnimal.intelligence / (liveAnimalIntelCount + myAnimal.intelligence) - 1, 1);
				}	
			}
			if (getBones > 0)
			{
				var leftBones:int = ownerInfo.heirloomBoxBoneLeft - getBones;
				ServerHelper.getInstance().fetchHeirloomBox(ownerInfo.id, getBones, leftBones, onFetchHeirloomBoxComplete);
			}
		}
		
		public function onFetchHeirloomBoxComplete(result:String, getBones:int):void
		{
			if (result == "success")
			{
				var message:String = "";
				if (PlayerInfoHelper.getInstance().userId != ownerInfo.id)
				{
					var me:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
					var myAnimal:AnimalInfo = PlayerInfoHelper.getInstance().animals[me.ownAnimals[0]];
					var liveAnimalIntelCount:int = 0;
					for (var i:int = 0; i < ownerInfo.liveAnimals.length; i++)
					{
						var liveAnimal:AnimalInfo =  PlayerInfoHelper.getInstance().animals[ownerInfo.liveAnimals[i]];
						liveAnimalIntelCount += liveAnimal.intelligence;
					}
					message += "你和驻守宝箱的动物智力比拼" + myAnimal.intelligence + "：" + liveAnimalIntelCount + "\n";
				}
				message += "收获骨头" + getBones +"根!";
				var messageBox:MessageBox = new MessageBox(message, 1);
				messageBox.x = (BFont.stageWidth - messageBox.width) / 2;
				messageBox.y = (BFont.stageHeight - messageBox.height) / 2;			
				SceneContainer.getInstance().currentScene.addChild(messageBox);
				refresh();
			}
		}
		
		private function onMouseOver(event:Event):void
		{
			mouseOverFlag = true;
			Mouse.cursor = "button";
			var AnimalMsgBgr:Class = ResourceHelper.getInstance().getClassByName("DialogMsgBgrImage");
			mouseOverMessage = new AnimalMsgBgr();
			mouseOverMessage.x = this.x + 20;
			mouseOverMessage.y = this.y - mouseOverMessage.height;
			SceneContainer.getInstance().currentScene.addChild(mouseOverMessage);
			
			tipText = new TextField();
			tipText.selectable = false; 
			
        	tipText.autoSize = TextFieldAutoSize.LEFT;
			tipText.defaultTextFormat = new TextFormat("微软雅黑", 14, 0x000000);
			tipText.wordWrap = false;
			tipText.x = 18;
			tipText.y = 2;
			mouseOverMessage.addChild(tipText);
			refresh();
		}

		private function refresh():void
		{
			var ownerRankInfo:RankConfig = ownerInfo.getRankInfo();
			var tipStr:String = "容量（" + ownerRankInfo.title + "用户）：" + ownerRankInfo.heirloomBoxVolume + "\n剩余：" + ownerInfo.heirloomBoxBoneLeft;
			if (ownerInfo.heirloomBoxBoneLeft > 0)
			{
				if (ownerInfo.id == PlayerInfoHelper.getInstance().userId)
				{
					tipStr += "\n快点击收取吧~~";
				}
				else if (ownerInfo.heirloomBoxBoneLeft == ownerRankInfo.heirloomBoxVolume)
				{
					tipStr += "\n快速下手吧~~";
				}
				else
				{
					tipStr += "\n已被盗过~~";
				}
				if (ownerInfo.heirloomBoxBoneLeft == ownerRankInfo.heirloomBoxVolume)
				{
					fullBones.visible = true;
					halfBones.visible = false;
				}
				else
				{
					fullBones.visible = false;
					halfBones.visible = true;
				}
			}
			else
			{
				var leftTime:int = ownerInfo.heirloomBoxFetchTime + Config.heirloomBoxFullTime - ServerHelper.getInstance().now();
				tipStr += "\n距离下次收获：" + Clock.formatTimeToSecond(leftTime);
				fullBones.visible = false;
				halfBones.visible = false;
			}
			if (tipText != null)
			{
				tipText.text = tipStr;
			}
		}
		
		private function onMouseOut(event:Event):void
		{
			mouseOverFlag = false;
			Mouse.cursor = "auto";
			if (mouseOverMessage.parent != null)
			{
				mouseOverMessage.parent.removeChild(mouseOverMessage);
			}
		}
	}
}